package com.chain.qs.modules.biz.service;

import com.chain.qs.common.persistence.Page;
import com.chain.qs.common.schedule.QuartzJobBean;
import com.chain.qs.common.service.CrudService;
import com.chain.qs.modules.biz.dao.EquSwitcherDao;
import com.chain.qs.modules.biz.entity.Condition;
import com.chain.qs.modules.biz.entity.EquSwitcher;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时开关机Service
 *
 */
@Service
//@Transactional(readOnly = true)
public class EquSwitcherService extends CrudService<EquSwitcherDao, EquSwitcher> {

    //定时任务组名标识，用于修改或更新任务
    private final static String JobGroupName = "equSwitch";

    @Resource
    private JobOperationService switcherTaskService;

    public List<EquSwitcher> switcherList(String equMapId) {
        return dao.switcherList(equMapId);
    }

    public String getEquNameById(String equMapEquid) {
        return dao.getEquNameById(equMapEquid);
    }

    private ArrayList<QuartzJobBean> parseToJobBeanList(List<EquSwitcher> switcherList) {
        ArrayList<QuartzJobBean> list = Lists.newArrayList();
        if (switcherList.size() == 0) {
            return list;
        }

        int j = 0;
        for (EquSwitcher equSwitcher : switcherList) {
            String[] weekArray = equSwitcher.getTurnWeek().split(",");
            for (int i = 0; i < weekArray.length; i++, j++) {
                StringBuilder sb = new StringBuilder();//cronTrigger expression
//            StringBuilder sb = new StringBuilder("0/1 0 0 * * ?");//cronTrigger expression
                sb.append("0 ");//add second
                sb.append(equSwitcher.getTurnMinute()).append(" ");//add minute
                sb.append(equSwitcher.getTurnHour()).append(" ");//add hour
                sb.append("? ");//add day-of-month
                sb.append("* ");//add month
                sb.append(weekArray[i]);//add day-of-week

                logger.debug("cronExpression is " + sb.toString());//print expression

                QuartzJobBean quartzJobBean = new QuartzJobBean();

                quartzJobBean.setCronExpression(sb.toString());
                quartzJobBean.setDescription(equSwitcher.getRemarks());
                quartzJobBean.setOperate(equSwitcher.getOperate());
                quartzJobBean.setJobGroup(JobGroupName);
                quartzJobBean.setJobName(equSwitcher.getOfficeName()+equSwitcher.getSgroupName()+equSwitcher.getGitemName() + j);
                quartzJobBean.setJobStatus(2);//任务初始状态 0禁用 1启用 2删除
                list.add(0, quartzJobBean);
            }
        }
        return list;
    }

    public ArrayList<QuartzJobBean> getQuartzJobBeanList() {
        List<EquSwitcher> switcherList = dao.findList(new EquSwitcher());
        return parseToJobBeanList(switcherList);
    }

    public Page<EquSwitcher> findPage(Page<EquSwitcher> page, EquSwitcher entity, Condition condition) {
        entity.setSgroupId(condition.getSgroupId());
        entity.setGitemId(condition.getGitemId());
        entity.setPage(page);
        page.setList(dao.findList(entity));
        return page;
    }

    // 删除所有任务
    private void refreshDel(){
        try {
            ArrayList<QuartzJobBean> switcherListPre = getQuartzJobBeanList();
            for (QuartzJobBean cur : switcherListPre) {
                switcherTaskService.delJob(cur);
            }
        } catch (Exception e) {
            System.out.println("删除后刷新失败");
        }
    }
    //重新初始化任务
    public void refreshInit(){
        try {
            refreshDel();
            ArrayList<QuartzJobBean> switcherListSuf = getQuartzJobBeanList();
            for (QuartzJobBean cur : switcherListSuf) {
                switcherTaskService.initJob(cur);
            }
        } catch (Exception e) {
            System.out.println("重新初始化失败");
        }
    }
}
